
package DBLayer;
import java.sql.*;

/**
 * @Author Gruppe 4
 * @Version 06-12-2011
 * This class is used to create the connection to the database
 * It is implemented as a singleton. The constructor is private to ensure that only
 * one object of the class is generated
 * Version for Sql Server or MySql lines in comments
 */

public class DBConnection 
{
    //constans used to get access to the database
    //SQL Server
    private static final String  driver = "jdbc:sqlserver://localhost:1433";
    private static final String  databaseName = ";databaseName=HimmerLandsKloak";
    private static String  userName = "; user=sa";
    private static String password = ";password=masterkey";
    private DatabaseMetaData dma;
    private static Connection con;
   
    //an instance of the class is generetated
    private static DBConnection  instance = null;

    //the constructor is private to ensure that only one object of this class is createt
    private DBConnection()
    {
    	String url = driver + databaseName + userName + password;

        try
        {
            //load the driver
            //SQL Server
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            System.out.println("Load of class ok");
        }//end try
        
        catch(Exception e)
        {
            System.out.println("Can not find the driver");
            System.out.println(e.getMessage());
        }//end catch
        
        try
        {
            //connect to the database
            con = DriverManager.getConnection(url);
            //set autocommit
            con.setAutoCommit(true);
            //get meta data
            dma = con.getMetaData(); 
            System.out.println("Connection to " + dma.getURL());
            System.out.println("Driver " + dma.getDriverName());
            System.out.println("Database product name " + dma.getDatabaseProductName());
        }//end try
        
        catch(Exception e)
        {
            System.out.println("Problems with the connection to the database");
            System.out.println(e.getMessage());
            System.out.println(url);
        }//end catch
    }//end  constructor
	   
    //closeDb: close the connection to the database
    public static void closeConnection()
    {
       	try
        {
            con.close();
            System.out.println("The connection is closed");
        }//end try
        
        catch (Exception e)
        {
           System.out.println("Error trying to close the database " +  e.getMessage());
        }//end catch
    }//end closeDB
		
    //getDBcon: GetMethod that returns the connection to the database
    public  Connection getDBcon()
    {
       return con;
    }
    
    //this method is used to get the instance of the connection
    public static DBConnection getInstance()
    {
        if (instance == null)
        {
          instance = new DBConnection();
        }
        return instance;
    }
    
    public static void startTransaction()
    { try{
        con.setAutoCommit(false);
        System.out.println("Transaction started");
        }
      catch(Exception e){
        System.out.println("fejl start transaction");
        System.out.println(e.getMessage());
      }
    }
    public static void commitTransaction()
    { try{
        con.setAutoCommit(true);
        }
      catch(Exception e){
        System.out.println("fejl commit transaction");
        System.out.println(e.getMessage());
      }
    }
    public static void rollbackTransaction()
    { try{
        con.rollback();
        con.setAutoCommit(true);
        System.out.println("Rolling back");
        }
      catch(Exception e){
        System.out.println("fejl rollback transaction");
        System.out.println(e.getMessage());
      }
    }
}